package com.gaoqi.mq.direct;


import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class DirectConsumer {
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(value = "direct",type = "direct"),
                    key = {
                          "admin.info"
                    }
            )
    })
    public void receive1MessageFromDirect(Channel channel, Message message){
        System.out.println("消费线程1消费:"+new String(message.getBody()));
    }

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue,
                    exchange = @Exchange(value = "direct",type = "direct"),
                    key = {
                        "user.info"
                    }
            )
    })
    public void receive2MessageFromDirect(Channel channel, Message message){
        System.out.println("消费线程2消费:"+new String(message.getBody()));
    }


//    /**
//     * 测试  TTL
//     * @param channel 信道
//     * @param message 消息
//     */
//    @RabbitListener(bindings = {
//            @QueueBinding(
//                    value = @Queue("dead_direct_queue"),
//                    exchange = @Exchange(value = "dead_direct_exchange",type = "direct"),
//                    key = {
//                            "dead"
//                    }
//            )
//    })
//    public void receive3MessageFromDirect(Channel channel, Message message){
////        try {
////            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
////            //手动确认
////            System.out.println("消费线程3消费:"+new String(message.getBody()));
////        } catch (IOException e) {
////            e.printStackTrace();
////            System.out.println("逻辑处理");
////        }
//        System.out.println("消费线程3消费:"+new String(message.getBody()));
//    }
}
